//
//  tableHeaderView.m
//  SeseNews
//
//  Created by lanou3g on 15/7/22.
//  Copyright (c) 2015年 Sese工作室. All rights reserved.
//

#import "tableHeaderView.h"
#import "model.h"
#import "UIImageView+WebCache.h"
#import "pageControl.h"
@interface tableHeaderView()
@property(nonatomic,strong)NSTimer *time;
@property(nonatomic,assign)BOOL isDragging;
@property(nonatomic,strong)pageControl *pageControl1;
@property(nonatomic,assign)int count;
@property(nonatomic,assign)NSUInteger currentImageIndex;
@property(nonatomic,strong)UIImageView *leftImageView;
@property(nonatomic,strong)UIImageView *rightImageView;
@property(nonatomic,strong)UIImageView *currentView;
@property(nonatomic,assign)int i;
@end
@implementation tableHeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)setTableHeaderV:(NSMutableArray *)tableHeaderV{
    CGFloat width=self.bounds.size.width;
    //    CGFloat height=self.bounds.size.height;
    _tableHeaderV=tableHeaderV;
    //    self.alpha=0;
    //滑到顶部.视图能否回到头部。
    self.scrollsToTop=YES;
    //是否整屏翻滚.默认为no
    self.pagingEnabled=YES;
    //边界是否回弹
    self.bounces=YES;
    //画面是否滚动
    self.scrollEnabled=YES;
    //水平的scroll滚动条隐藏
    self.showsHorizontalScrollIndicator=NO;
    //边界设置为no
    self.bounces=NO;
    self.count=(int)[self.tableHeaderV count];
    self.contentSize=CGSizeMake(self.bounds.size.width*(_count+2),250);
    //左视图创建，数组中最后的一条数据
    self.leftImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 250)];
    model *modelLeft=[self.tableHeaderV objectAtIndex:_count-1];
    [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:modelLeft.thumbnail]];
    model *model1=[self.tableHeaderV objectAtIndex:(_count-1)];
    UILabel *titleLebel1=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, 375, 35)];
    titleLebel1.text=model1.title;
    int i=self.count-1;
    UIImageView *baseBoardLeft=[[UIImageView alloc]initWithFrame:CGRectMake(0, 215, 375,35)];
      baseBoardLeft.backgroundColor=[UIColor colorWithWhite:1.000 alpha:0.500];
    [baseBoardLeft addSubview:titleLebel1];
    [self.leftImageView addSubview:baseBoardLeft];
    [self.leftImageView addSubview:[self creatPageControl:i]];
    [self addSubview:self.leftImageView];
    
    //右视图创建，数据中第一个数据
    self.rightImageView=[[UIImageView alloc]initWithFrame:CGRectMake((_count+1)*self.bounds.size.width, 0, 375, 250)];
    model *modelRight=[self.tableHeaderV objectAtIndex:0];
    [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:modelRight.thumbnail]];
    model *model2=[self.tableHeaderV objectAtIndex:0];
    //创建label
    UILabel *titleLebel2=[[UILabel alloc]initWithFrame:CGRectMake(10,5, 375, 35)];
    titleLebel2.text=model2.title;
     //创建背景图，
    UIImageView *baseBoardRight=[[UIImageView alloc]initWithFrame:CGRectMake(0,215, 375,35)];
    baseBoardRight.backgroundColor=[UIColor colorWithWhite:1.000 alpha:0.500];
    //先添加背景图
    [baseBoardRight addSubview:titleLebel2];
    [self.rightImageView addSubview:baseBoardRight];
    [self.rightImageView addSubview:[self creatPageControl:0]];
    [self addSubview:self.rightImageView];
    
    for (int i=0; i<_count; i++) {
        int x=i+1;
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(width*x, 0, 375, 250)];
        
        model *model=[self.tableHeaderV objectAtIndex:i];
        UILabel *titleLebel=[[UILabel alloc]initWithFrame:CGRectMake(10,5, imageView.frame.size.width,35)];
        
        UIImageView *baseBoard=[[UIImageView alloc]initWithFrame:CGRectMake(0, 215, 375,35)];
        baseBoard.backgroundColor=[UIColor colorWithWhite:1.000 alpha:0.500];
        titleLebel.text=model.title;
        titleLebel.backgroundColor= [UIColor clearColor];
        [baseBoard addSubview:titleLebel];
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.thumbnail]];
        imageView.contentMode=UIViewContentModeScaleToFill;
        [imageView addSubview:baseBoard];
        [imageView addSubview:[self creatPageControl:i]];
        [self addSubview:imageView];
    }
    self.contentOffset=CGPointMake(self.bounds.size.width, 0);
    if (self.count!=1) {
        [self setupTimer];
    }
    self.delegate=self;
    
    
    
    
    
    
}
//创建pagecontrol
-(pageControl*)creatPageControl:(int)i{
    //    创建uipageControl
    self.pageControl1=[[pageControl alloc]init];
    self.pageControl1.numberOfPages=_count;
    
//    CGSize size=[self.pageControl1 sizeForNumberOfPages:_count];
    self.pageControl1.bounds=CGRectMake(0, 0,50,20);
    self.pageControl1.center=CGPointMake(335,240);
    self.pageControl1.currentPage=i;
    [self.pageControl1 setPageIndicatorImage:[UIImage imageNamed:@"page3_7.png"]];
   	[self.pageControl1 setCurrentPageIndicatorImage:[UIImage imageNamed:@"page3_9.png"]];
    self.pageControl1.indicatorMargin=10.f;
//    self.pageControl1.currentPageIndicatorTintColor=[UIColor blackColor];
//    self.pageControl1.pageIndicatorTintColor=[UIColor grayColor];
//    [self.pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
    return self.pageControl1;
}
-(void)pageChanged:(UIPageControl*)pageControl{
    self.pageControl1.currentPage=self.contentOffset.x/self.bounds.size.width;
    //    self.contentOffset=CGPointMake(self.bounds.size.width*self.i, 0);
}
//创建定时器
-(void)setupTimer{
    self.time=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(update:) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.time forMode:NSRunLoopCommonModes];
}

- (void)update:(NSTimer *)timer{
    if (_isDragging == YES) {
        return ;
    }
    CGPoint offSet = self.contentOffset;
    if (offSet.x >= self.bounds.size.width *(_count+1)) {
        
        self.contentOffset=CGPointMake(self.bounds.size.width, 0);
        
    }else{
        offSet.x +=self.bounds.size.width;
        [self setContentOffset:offSet animated:YES];
        
    }
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    //停止定时器
    [self.time invalidate];
    self.time=nil;
//     [self.time setFireDate:[NSDate distantFuture]];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (self.contentOffset.x==((_count+1)*self.bounds.size.width)) {
        self.contentOffset=CGPointMake(self.bounds.size.width, 0);
        self.i=0;
    }
    else if (self.contentOffset.x==0) {
        self.contentOffset=CGPointMake(_count*self.bounds.size.width, 0);
        self.i=self.count;
    }
    if (self.count!=1) {
        [self setupTimer];
    }
}












@end
